/*
 * 功能:提供div弹出窗口
 * 1.windowOpen 打开div弹出窗口方法
 * 2.windowClose 关闭div弹出窗口方法
 * 3.windowResize 改变div弹出窗口大小方法,用于上一步和下一步操作使用
 * 4.forwardAndResize 作用相当于转跳页面然后改变窗口大小
 * 5.backAndResize 相当于浏览器的history.back()功能然后改变窗口大小
 * @author 钟永生
 * @version 1.1
 */
//弹出窗口
var win;
var windowOpen;
var windowClose;
var windowResize;
/**
* @deprecated
*/
if(top != this){
		if(windowOpen == undefined){
			//alert('windowOpen == undefined');
			windowOpen =  top.windowOpen;
		}
		if(windowClose == undefined){
			//alert('windowClose == undefined');
			windowClose = top.windowClose;
		}
		if(windowResize == undefined){
			//alert('windowResize == undefined');
			windowResize = top.windowResize;
		}
}else{
	/**
	 * 打开div弹出窗口方法
	 * @param url 弹出窗口的连接
	 * @param winid 弹出窗口id
	 * @param winHeight 弹出窗口的高度
	 * @param winWidth 弹出窗口的宽度
	 * @param modal 是否为模态窗,默认为true,即此窗口不关闭,不能进行之前的页面操作,true为模态窗,false为普通窗口
	 * @param ifid 弹出窗口iframe的id
	 */
	windowOpen = function(url, winId, winTitle, winHeight, winWidth, modal, ifid){
		if(win != null && win.isVisible() && win == Ext.WindowMgr.get(winId) ){
			Ext.WindowMgr.bringToFront(win);
			return;
		}
		var winHeight = (winHeight== null||winHeight==undefined)? 300: winHeight;
		var winWidth = (winWidth== null||winWidth==undefined)? 500: winWidth;
		var inject = "if(this.src!='about:blank' && readyState == 'interactive'){" +
				"contentWindow.windowOpen=windowOpen;" +
				"contentWindow.windowClose=windowClose;" +
				"contentWindow.windowResize=windowResize;" +
				"contentWindow.forwardAndResize=forwardAndResize;" +
				"contentWindow.backAndResize=backAndResize;}";
	    var iframe = {
	    tag: "iframe"
	    ,id:ifid||(winId+'Ifm')
	    ,name:ifid||(winId+'Ifm')
	    ,src:url||''
	    ,height:"100%"
	    ,width:"100%"
	    ,frameborder:"0"
	    ,scrolling:"auto"
	    ,onreadystatechange:inject
	    //,onload:inject
		};
		win = new Ext.Window({
            	id: winId,
            	title: winTitle || '',
                layout: 'fit',
                width:winWidth ,
                height:winHeight ,
                modal: modal || true,
                autoScroll: true,
                constrain: true,
				onEsc:Ext.emptyFn,
				html:iframe
				//el :ifid
        });
		win.on('resize', function(panel, width, height){
			//alert('resize : ' + width + ','+height);
			var ifa = Ext.get(iframe.id);
			var ifh = panel.getInnerHeight();
			var ifw = panel.getInnerWidth();
			ifa.setHeight(ifh);
			ifa.setWidth(ifw);
		});
		win.on('beforeshow', function(thisWin){
			var maxWidth = Ext.lib.Dom.getViewWidth(true);
			var maxHeight = Ext.lib.Dom.getViewHeight(true);
			if(thisWin.width > maxWidth ){
				thisWin.setWidth(maxWidth - thisWin.getFrameWidth()/1.5);
			}
			if(thisWin.height > maxHeight){
				thisWin.setHeight(maxHeight - thisWin.getFrameHeight()/1.5);
			}
		});
		win.on('beforeclose', function(thisWin){
			//清空iframe
			var iframe = thisWin.body.dom.firstChild;
			//alert(iframe.src);
			if (iframe.src) {
				iframe.src = "about:blank";
				//alert(iframe.src);
				//alert(iframe.contentWindow.document.body.innerHTML);
			}
		});
        win.show();

        var ifObj = win.body.dom.firstChild;
        ifObj.win = win;
       	ifObj.windowOpen = top.windowOpen;
       	/**
		* 关闭div弹出窗口方法
	 	* @param winId 要关闭的窗口id
	 	*/
        ifObj.windowClose = function(){
        	if(arguments.length == 1 && typeof arguments[0] == 'string'){
				var closeWin = Ext.WindowMgr.get(arguments[0]);
				if(closeWin == null) return;
				closeWin.close();
        	}else{
        		ifObj.win.close();
        	}
        }

		/**
		 * 改变div弹出窗口大小方法,用于上一步和下一步操作使用
		 * @param winId 要改变大小的窗口id
		 * @param heigth 窗口高度
		 * @param width 窗口宽度
		 */
        ifObj.windowResize = function(){
        	if(arguments.length == 3 && typeof arguments[0] == 'string'){
				var resizeWin = Ext.WindowMgr.get(winId);
				resizeWin.setHeight(arguments[1]);
				resizeWin.setWidth(arguments[2]);
				resizeWin.center();
        	}
        	if(arguments.length == 2){
        		ifObj.win.setHeight(arguments[0]);
				ifObj.win.setWidth(arguments[1]);
				ifObj.win.center();
        	}
        }

        /**
         * 相当于转跳页面然后改变窗口大小
         * @param url 跳转的url连接
         * @param heigth 窗口高度
         * @param width 窗口宽度
         */
        ifObj.forwardAndResize = function(){
        	if(arguments.length == 3 && typeof arguments[0] == 'string'){
        		ifObj.contentWindow.location.href=arguments[0];
        		ifObj.windowResize(arguments[1], arguments[2]);
        	}
        }

        /**
         * 相当于浏览器的history.back()功能然后改变窗口大小
         * @param heigth 窗口高度
         * @param width 窗口宽度
         */
        ifObj.backAndResize = function(){
        	ifObj.contentWindow.history.back();
        	ifObj.windowResize(arguments[1], arguments[2]);
        }
	}

	/**
	* 关闭div弹出窗口方法
	* @deprecated 用于兼容过去版本
	*
 	* @param winId 要关闭的窗口id
 	*/
  	windowClose = function(){
       	if(arguments.length == 1 && (typeof arguments[0]) == 'string'){
			var closeWin = Ext.WindowMgr.get(arguments[0]);
			if(closeWin == null) return;
			closeWin.close();
       	}
   	}

	/**
	 * 改变div弹出窗口大小方法,用于上一步和下一步操作使用
	 * @deprecated 用于兼容过去版本
	 *
	 * @param winId 要改变大小的窗口id
	 * @param heigth 窗口高度
	 * @param width 窗口宽度
	 */
	windowResize = function(){
      	if(arguments.length == 3 && (typeof arguments[0]) == 'string'){
			var resizeWin = Ext.WindowMgr.get(arguments[0]);
			resizeWin.setHeight(arguments[1]);
			resizeWin.setWidth(arguments[2]);
			resizeWin.center();
       	}
	}
}